#
#
#   Generate a mesh
#
#

from panda3d.core import Texture, GeomNode, CollisionPolygon
from panda3d.core import GeomVertexFormat, GeomVertexData
from panda3d.core import Geom, GeomTriangles, GeomVertexWriter
from panda3d.core import Vec3, Vec4, Point3
import time

def myNormalize(myVec):
    myVec.normalize()
    return myVec

class MeshGenerator():
    
    def __init__(self, name = 'Mesh'):
        
        self.name = name
        self.finished = False

        self.format = GeomVertexFormat.getV3c4t2()
        #print self.format
        self.vertexData = GeomVertexData(name, self.format, Geom.UHStatic)
        self.vertexData.setNumRows(26136)
        
        self.mesh = Geom(self.vertexData)
        self.triangles = GeomTriangles(Geom.UHStatic)
        self.triangleData = self.triangles.modifyVertices()
        self.triangleData.setNumRows(26136)
        
        self.vertex = GeomVertexWriter(self.vertexData, 'vertex')
        self.normal = GeomVertexWriter(self.vertexData, 'normal')
        self.color = GeomVertexWriter(self.vertexData, 'color')
        self.texcoord = GeomVertexWriter(self.vertexData, 'texcoord')
        
        self.faceCount = 0
    
    def makeFace(self, x1, y1, z1, x2, y2, z2, id, color):
        
        if x1 != x2:
        
            self.vertex.addData3f(x1, y1, z1)
            self.vertex.addData3f(x2, y1, z1)
            self.vertex.addData3f(x2, y2, z2)
            self.vertex.addData3f(x1, y2, z2)
            quad = CollisionPolygon(Point3(x1, y1, z1), Point3(x2, y1, z1),
                                     Point3(x2, y2, z2), Point3(x1, y2, z2))
        
        else:
        
            self.vertex.addData3f(x1, y1, z1)
            self.vertex.addData3f(x2, y2, z1)
            self.vertex.addData3f(x2, y2, z2)
            self.vertex.addData3f(x1, y1, z2)
            quad = CollisionPolygon(Point3(x1, y1, z1), Point3(x2, y2, z1),
                                    Point3(x2, y2, z2), Point3(x1, y1, z2))


        self.color.addData4f(color, color, color, 1.0)
        self.color.addData4f(color, color, color, 1.0)
        self.color.addData4f(color, color, color, 1.0)
        self.color.addData4f(color, color, color, 1.0)
        
        if id == 0:
            self.texcoord.addData2f(0.0, 1.0)
            self.texcoord.addData2f(0.0, 0.0)
            self.texcoord.addData2f(0.5, 0.0)
            self.texcoord.addData2f(0.5, 1.0)
        elif id == 1:
            self.texcoord.addData2f(0.5, 1.0)
            self.texcoord.addData2f(0.5, 0.0)
            self.texcoord.addData2f(1.0, 0.0)
            self.texcoord.addData2f(1.0, 1.0)
        
        vertexId = self.faceCount * 4
        
        self.triangles.addVertices(vertexId, vertexId + 1, vertexId + 3)
        self.triangles.addVertices(vertexId + 1, vertexId + 2, vertexId + 3)
        
        self.faceCount += 1
        return quad
    
    def makeFrontFace(self, x, y, z, id):
        quad = self.makeFace(x + 1, y + 1, z, x, y + 1, z + 1, id, 0.6)
        return quad
    
    def makeBackFace(self, x, y, z, id):
        quad = self.makeFace(x, y, z, x + 1, y, z + 1, id, 0.85)
        return quad
    def makeRightFace(self, x, y, z, id):
        quad = self.makeFace(x + 1, y, z, x + 1, y + 1, z + 1, id, 1.0)
        return quad
    def makeLeftFace(self, x, y, z, id):
        quad = self.makeFace(x, y + 1, z, x, y, z + 1, id, 0.9)
        return quad
    def makeTopFace(self, x, y, z, id):
        quad = self.makeFace(x + 1, y + 1, z + 1, x, y, z + 1, id, 1.0)
        return quad
    def makeBottomFace(self, x, y, z, id):
        quad = self.makeFace(x, y + 1, z, x + 1, y, z, id, 0.70)
        return quad
    def getMesh(self):
        return self.mesh
        return quad
    def getGeomNode(self):
        if self.finished == False:
            self.triangles.closePrimitive()
            self.mesh.addPrimitive(self.triangles)
            self.finished = True
        geomNode = GeomNode(self.name)
        geomNode.addGeom(self.mesh)
        return geomNode
        
